package authority.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import authority.comm.Lay;
import authority.comm.Message;
import authority.entity.Role;
import authority.entity.User;
import authority.service.RoleService;
import authority.service.UserService;
import authority.util.CommonUtil;
import authority.util.DateUtil;

import com.alibaba.fastjson.JSON;
import org.apache.log4j.Logger;

/**
 * 用户servlet
 * @author shichenyang
 *
 */
@WebServlet(urlPatterns="/userServlet")
public class UserServlet extends HttpServlet {

	private static Logger logger=Logger.getLogger(UserServlet.class);
	private static final long serialVersionUID = 1967652951018694974L;
	private UserService service=new UserService();
	private RoleService roleService=new RoleService();

	public UserServlet() {
		super();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String opr=request.getParameter("opr");//控制器
		
		
		if("login".equals(opr)){//登陆
			String name=request.getParameter("username");
			String password=request.getParameter("password");
			User user=new User();
			user.setName(name);
			user.setPassword(password);
			
			Message message= service.login(user, request);
			out.print(JSON.toJSONString(message));
		}
		
		if("addUser".equals(opr)){//添加用户
			String name=request.getParameter("name");
			String pwd=request.getParameter("password");
			String birthday=request.getParameter("birthday");
			String [] roles=request.getParameterValues("role");
			String realName=request.getParameter("realName");
			String roleId=request.getParameter("roleId");
			String deptId=request.getParameter("deptId");
			User user=new User();
			user.setName(name);
			user.setPassword(pwd);
			user.setRealName(realName);
			user.setRoleId(roleId);
			user.setDeptId(deptId);
			try {
				user.setBirthday(DateUtil.convertDate(birthday, DateUtil.Y2));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			Message message= service.addUser(user, roles, (User) request.getSession().getAttribute("user"));
			out.print(JSON.toJSONString(message));
		}
		if("getUser".equals(opr)){//获取登陆人
			User user= (User) request.getSession().getAttribute("user");
			out.print(JSON.toJSONString(user));
		}
		
		if("addBefore".equals(opr)){//添加用户之前，加载所有的角色信息
			List<Role> l= roleService.findAllRole();
			request.setAttribute("list", l);
			request.getRequestDispatcher("/user/user-add.jsp").forward(request, response);
		}
		
		if("findList".equals(opr)){//分页获取用户列表
			//1.获取了分页信息
			String page=request.getParameter("page");//当前页
			String startDate=request.getParameter("startDate");//查询条件，暂时没用
			String endDate=request.getParameter("endDate");//查询条件，暂时没用
			String name=request.getParameter("name");//查询条件，暂时没用
			String limit=request.getParameter("limit");//显示条数
			Integer p=CommonUtil.objectToInteger(page);
			Integer li=CommonUtil.objectToInteger(limit);
			
			User user=new User();
			user.setName(name);
			User u=(User)request.getSession().getAttribute("user");
			user.setRoleId(u.getRoleId());
			user.setDeptId(u.getDeptId());
			user.setId(u.getId());
			//2.使用分页帮助类
			Lay lay=new Lay();
			lay.setPage(p);
			lay.setLimit(li);
			lay= service.findListByPage(lay, user, startDate, endDate);
			//3.输出json数据
			out.print(JSON.toJSONString(lay));//将date数据进行格式化
		}

		if ("update".equals(opr)) {//更新某个用户
			String id=request.getParameter("id");
			String name=request.getParameter("name");
			String pwd=request.getParameter("password");
			String birthday=request.getParameter("birthday");
			String [] roles=request.getParameterValues("role");
			String realName=request.getParameter("realName");
			String roleId=request.getParameter("roleId");
			String deptId=request.getParameter("deptId");
			User user=new User();
			user.setId(id);
			user.setName(name);
			user.setPassword(pwd);
			user.setRealName(realName);
			user.setRoleId(roleId);
			user.setDeptId(deptId);
			try {
				user.setBirthday(DateUtil.convertDate(birthday, DateUtil.Y2));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			logger.info(user.getId());
			logger.info(user.toString());
			Message message= service.updateUser(user, roles, (User) request.getSession().getAttribute("user"));
			out.print(JSON.toJSONString(message));
		}

		if("del".equals(opr)){//删除某个用户
			String id=request.getParameter("id");
			Message m= service.delUser(id);
			out.print(JSON.toJSONString(m));
		}
		
		if("exp".equals(opr)){//导出excel
			response.reset();//重置response不会报错
			String startDate=request.getParameter("startDate");
			String endDate=request.getParameter("endDate");
			String name=request.getParameter("name");
			User user=new User();
			user.setName(name);
			service.expUser(user, startDate, endDate, response);
		}
		out.flush();
		out.close();
	}
}
